package vn.edu.saigontech.ncms.web;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.util.DateUtil;

public class View {

	public static String trimPhoneNumber(String text) {
		StringBuilder stringBuilder = new StringBuilder();
		try {
			String[] tokens = text.split("[-/,]");
			for (String string : tokens) {
				stringBuilder.append(string + "\n");
			}
		} catch (Exception ex) {
		}
		return stringBuilder.toString();
	}

	/**
	 * get the first string to capitalize it in receipt_print.ftl
	 * 
	 * @param string
	 * @return
	 */
	public static String getFirstString(String string) {
		int index = string.indexOf(" ");
		if (index > 0)
			return string.substring(0, index);
		return "";
	}

	/**
	 * get the retain string from the second string
	 * 
	 * @param string
	 * @return
	 */
	public static String getFromSecondString(String string) {
		int index = string.indexOf(" ");
		if (index > 0)
			return string.substring(index + 1, string.length()) + "đồng";
		return "";
	}

	public static String printTeacher(List<Teacher> teachers) {
		if (teachers.size() == 0)
			return "";
		if (teachers.size() == 1)
			return teachers.iterator().next().getFullName();
		StringBuilder result = new StringBuilder();
		Iterator<Teacher> iterator = teachers.iterator();
		while (iterator.hasNext()) {
			String fullName = iterator.next().getFullName();
			if (iterator.hasNext())
				result.append(fullName + " & ");
			else
				result.append(fullName);
		}
		return result.toString();
	}
	
	public static long compareDate(Date startDate, Date endDate) {
		return DateUtil.getDifferenceDate(endDate, startDate)-1;
	}
	
	public static String dateToVNString(Date date) {
		return "ngày " + DateUtil.getDayOfMonth(date) + " tháng " + (DateUtil.getMonth(date) + 1) + 
			" năm " + DateUtil.getYear(date);
	}
	
	public static String fullDateToVNString(Date date) {
		return DateUtil.dateOfWeekVN(date) + " (" + DateUtil.getDayOfMonth(date) 
			+ "/" + (DateUtil.getMonth(date)+1) + "/" + DateUtil.getYear(date) + ")";
	}
	
	public static String weekDateVNString(Date date) {
		return DateUtil.dateOfWeekVN(date);
	}
	
	public static String studyWeekDateVNString(Date date, String lang) {
		String viLang = "vi";
		String enLang = "en";
		String result = "";
		Calendar cal = DateUtil.toCalendar(date);
		int value = cal.get(Calendar.DAY_OF_WEEK);
		if (value == Calendar.MONDAY || value == Calendar.WEDNESDAY || value == Calendar.FRIDAY) {
			if (lang.equals(viLang))
				result = "Thứ Hai - Thứ Tư - Thứ Sáu";
			else if (lang.equals(enLang))
				result = "Mon - Wed - Fri";
		}
		else if (value == Calendar.TUESDAY || value == Calendar.THURSDAY || value == Calendar.SATURDAY) {
			if (lang.equals(viLang))
				result = "Thứ Ba - Thứ Năm - Thứ Bảy";
			else if (lang.equals(enLang))
				result = "Tue - Thu - Sat";
		}
		return result;
	}
	
	public static String studyWeekDateENString(Date date) {
		return DateUtil.dateOfWeekVN(date);
	}
}